package com.dao.impl;

import com.bean.Disaster;
import com.bean.User;
import com.dao.UserDao;
import com.web.common.exception.DataAccessException;
import com.web.common.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author wangzhipeng
 * @Date 2020-08-31 10:29 上午
 * @Description
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User loginByPhone(String phone, String password) throws DataAccessException {
        Connection connection = DBUtils.getConnection();
        String sql = "select * from user where uphone = ? and upassword = ?";
        PreparedStatement st = null;
        ResultSet resultSet= null;
        int result = 0;
        User user = null;
        try {
            st = connection.prepareStatement(sql);
            st.setString(1,phone);
            st.setString(2,password);
            resultSet = st.executeQuery();
            if (resultSet.next()){
                result = 1;
                int id = resultSet.getInt("uid");
                String name = resultSet.getString("uname");
                String password2 = resultSet.getString("upassword");
                String email = resultSet.getString("uemail");
                String phone2 = resultSet.getString("uphone");
                user = new User(id,name,password2,phone2,email);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.close(resultSet,st,connection);
        }
        if (result == 0){
            return null;
        }else {
            return user;
        }


    }

    @Override
    public int registerUser(User user) throws DataAccessException {
        System.out.println("正在注册的用户信息为"+user.toString());
        Connection connection = DBUtils.getConnection();
        String sql = "insert into user(uname,upassword,uphone,uemail) values(?,?,?,?)";
        PreparedStatement preparedStatement = null;
        int result = 0;
        try {
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,user.getUname());
            preparedStatement.setString(2,user.getUpassword());
            preparedStatement.setString(3,user.getUphone());
            preparedStatement.setString(4,user.getUemail());
            preparedStatement.execute();
            result = 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.close(null,preparedStatement,connection);
        }

        return result;
    }

    @Override
    public int uploadDisaster(Disaster disaster) throws DataAccessException {
        LocalDateTime localDateTime = LocalDateTime.now();
         DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
         String time = localDateTime.format(dateTimeFormatter);
        System.out.println("提交时间"+time);

        Connection connection = DBUtils.getConnection();
        String sql = "insert into disaster(dtype,ddegree,dinfo,dlocation,dphone,uid,dtime) values(?,?,?,?,?,?,?)";
        PreparedStatement st = null;
        int result = 0;
        try {
            st = connection.prepareStatement(sql);
            st.setString(1,disaster.getDtype());
            st.setString(2,disaster.getDdegree());
            st.setString(3,disaster.getDinfo());
            st.setString(4,disaster.getDlocation());
            st.setString(5,disaster.getDphone());
            st.setInt(6,disaster.getUid());
            st.setString(7,time);
            st.execute();
            result = 1;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.close(null,st,connection);
        }

        return result;
    }

    @Override
    public List<Disaster> selectMyDisaster(Integer uid) throws DataAccessException {
        Connection connection = DBUtils.getConnection();
        String sql = "select * from disaster where uid = ?";
        PreparedStatement statement = null;
        ResultSet rs = null;
        List<Disaster> list = new ArrayList<>();
        try {
            statement = connection.prepareStatement(sql);
            statement.setInt(1,uid);
            rs = statement.executeQuery();
            while (rs.next()){
                Disaster disaster = new Disaster();
                disaster.setDid(rs.getInt("did"));
                disaster.setDtype(rs.getString("dtype"));
                disaster.setDdegree(rs.getString("ddegree"));
                disaster.setDinfo(rs.getString("dinfo"));
                disaster.setDlocation(rs.getString("dlocation"));
                disaster.setDphone(rs.getString("dphone"));
                disaster.setUid(rs.getInt("uid"));
                disaster.setTime(rs.getString("dtime"));
                list.add(disaster);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtils.close(rs,statement,connection);
        }
        return list;
    }
}
